package com.ruihang.generalibrary.ui.util;

import android.util.SparseArray;
import android.view.View;

import com.jiongbull.jlog.JLog;

import java.lang.ref.WeakReference;

/**
 * Created by rh008 on 2017/7/25.
 */

public class MultiModeViewWrapper<T extends View> {

    private WeakReference<T> wt;

    protected SparseArray<IViewMode<T>> modeList;

    public Integer currentMode;

    public MultiModeViewWrapper(T t) {
        this.wt = new WeakReference<T>(t);
        modeList = new SparseArray<>();
    }

    public MultiModeViewWrapper addMode(int mode, IViewMode<T> viewMode) {
        if (viewMode != null) {
            modeList.append(mode, viewMode);
            if (currentMode == null) {
            }
        }
        return this;
    }

    public MultiModeViewWrapper setMode(int mode) {
        if (this.currentMode != null && this.currentMode == mode) {
            return this;
        }
        IViewMode str = modeList.get(mode);
        if (str != null) {
            currentMode = mode;
            str.setShow(wt.get());
        } else {
            JLog.e("set mode failed, not have this mode : " + mode);
        }
        return this;
    }

    public Integer getCurrentMode() {
        return currentMode;
    }

    public interface IViewMode<T extends View>{
        void setShow(T t);
    }
}
